package com.ask.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class Knife4jConfig implements WebMvcConfigurer  {
    @Bean
    public Docket docket() {

        // 使用 ApiInfoBuilder 创建 API 的基本信息，如标题、版本和描述
        System.out.println("Swagger2配置类加载");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("Book接口文档")
                .version("2.0")
                .description("Book接口文档")
                .build();

        // 创建一个 Docket 对象，用于配置 Swagger 的主要设置
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("book管理接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ask.controller"))
                .paths(PathSelectors.any()) //  表示对所有路径生成文档
                .build();
        return docket;
    }

    // 配置静态资源路径

    public  void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("静态资源路径配置");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
